package mysql

import (
	"bluebell/models"
)

// CreatePost 添加一条帖子，添加一条详情信息
func CreatePost(p *models.Post) (err error) {
	sqlStr := `insert into post(post_id,title,content,author_id,community_id)values(?,?,?,?,?)`
	_, err = db.Exec(sqlStr, p.ID, p.Title, p.Content, p.AuthorID, p.CommunityID)

	return
}

// GetPostById 根据id查询该id详情页
func GetPostById(pid int64) (post *models.Post, err error) {
	post = new(models.Post)
	sqlStr := `select
	post_id,title,content,author_id,community_id,create_time
	from post
	where post_id = ?`

	err = db.Get(post, sqlStr, pid)
	return
}

// GetPostList 获取多个帖子的列表
func GetPostList(page, size int64) (posts []*models.Post, err error) {
	sqlStr := `select 
	post_id,title,content,author_id,community_id,create_time
	from post
	limit ?,?
	`
	posts = make([]*models.Post, 0, 2) // 不要写成make([]*models.Post,2)这种形式
	db.Select(&posts, sqlStr, (page-1)*size, size)
	return
}
